<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.w3.org/1999/xhtml">

<head>
    <th:block th:include="common/common_head :: commonHeader('配额管理')"/>
    <link rel="stylesheet" th:href="@{/static/js/plugins/jquery-layout/jquery.layout-latest.css}">
</head>
<body class="gray-bg">
<div class="container-div ui-layout-center">
    <div class="row">
        <div class="col-sm-12 search-condition">
            <form id="quotaForm" th:action="@{/quota/list}" class="form-inline">
                <!-- <input type="hidden" name="deptId" id="deptId"/> -->
                <div class="select-list">
                    <ul>
                        <li class="col-sm-3">
                            <label>供应商编码：</label>
                            <input type="text" name="vendorCode"/>
                        </li>
                        <li class="col-sm-3">
                            <label>物料编码：</label>
                            <input type="text" name="materialCode"/>
                        </li>
                        <li class="col-sm-3">
                            <label>工厂 ：</label>
                            <select name="werks" sysDict="GC" emptyFirst="true"></select>
                        </li>
                        <li class="col-sm-3">
                            <button id="quotaSearchBtn" type="button" class="btn btn-primary btn-rounded btn-sm"><i
                                    class="fa fa-search"></i>&nbsp;搜索
                            </button>
                            <button class="btn btn-warning btn-rounded btn-sm" type="reset"><i class="fa fa-eraser"></i>&nbsp;重置
                            </button>
                            <button id="quotaAdvancedSearch" type="button" class="btn btn-primary btn-rounded btn-sm"
                                    style="display: none"><i class="fa fa-caret-down"></i>&nbsp;高级搜索
                            </button>
                        </li>
                    </ul>

                </div>
            </form>
        </div>
        <div class="col-sm-12 select-info">
            <div class="col-sm-12 select-info">
                <div class="btn-group hidden-xs toolbar" id="quotaToolBar" role="group">
                    <a class="btn btn-outline btn-success btn-rounded" onclick="toquotaAdd()"
                       shiro:hasPermission="quota:save">
                        <i class="fa fa-plus"></i> 新增
                    </a>
                    <a class="btn btn-outline btn-success btn-rounded" onclick="toEditquotaInfo()">
                        <i class="fa fa-edit"></i> 编辑
                    </a>
                    <a class="btn btn-outline btn-success btn-rounded" onclick="downloadTemplate()">
                        <i class="fa fa-edit"></i> 模板
                    </a>
                    <a class="btn btn-outline btn-success btn-rounded" onclick="importByTemplate()">
                        <i class="fa fa-edit"></i> 导入
                    </a>
                    <!--<a class="btn btn-outline btn-danger btn-rounded" onclick="deletequota()">
                        <i class="fa fa-remove"></i> 删除
                    </a>-->
                </div>
                <table id="quota"></table>
            </div>
        </div>
    </div>
</div>

<!--上传模板-->
<div id="uploadModal" style="display: none">
    <div class="modal-body">
        <form id="uploadForm" enctype="multipart/form-data">
            <div style="margin-top:20px"><label style="color: red">*</label><label><b>选择文件：</b></label>
                <input type="file" name="importFile" id="importFile" style="display:inline">
            </div>
        </form>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-default" onclick='closeDialog()'>关闭</button>
        <button type="button" class="btn btn-primary" onclick='uploadTemplateData()'>确定</button>
    </div>
</div>
<!--上传模板结束-->

<!--引入公共js-->
<div th:include="common/onload_js :: onloadJS"></div>
<script type="text/javascript" th:src="@{/static/js/plugins/jquery-layout/jquery.layout-latest.js}"></script>

<!-- Page-Level Scripts -->

<!--  -->
<script type="text/javascript">
    //面向对象
    var searcherBS = Searcher.createBS('#quota',
        [
            {checkbox: true},
            {title: "物料号", field: "materialCode"},
            {title: "物料描述", field: "materialDesc"},
            {title: "供应商编码", field: "vendorCode"},
            {title: "供应商名称", field: "vendorName"},
            {title: "工厂", field: "werks"},
            {title: "配额比", field: "percentile"},
            {title: "配额数", field: "quotaNum"},
            {
                title: "剩余配额数", field: "usedQuotaNum",
                formatter: function (value, row, index) {
                    if (row.quotaNum) {
                        return row.quotaNum - value;
                    }
                }
            },
            {title: "创建者", field: "createdBy"},
            {title: "开始日期", field: "startDate"},
            {title: "结束日期", field: "endDate"},
            {title: "创建日期", field: "creationDate"}
        ]
    );
    $(function () {
        initDictDataSel();//初始化所有字典数据
    });

    /**
     * 查询数据
     */
    function refresh() {
        searcherBS.refresh();
    }

    /**
     * 跳转至新增页
     */
    function toquotaAdd() {
        var index = Dialog.openUrl("供应商配额", ctxPath + '/modules/quota/quota_add.html', {
            area: ['900px', '700px'],
            maxmin: true,
            end: function () {
            }
        });
        layer.full(index);
    }

    /**
     * 编辑
     */
    function toEditquotaInfo() {
        //field:"pkId"
        var rowDatas = searcherBS.getSelectedRows("quotaId");
        if (rowDatas.length != 1) {
            Dialog.errorMsg("请选择一行进行编辑");
            return;
        }
        var index = Dialog.openUrl("编辑供应商信息", ctxPath + '/quota/toEdit/' + rowDatas,
            {
                area: ['900px', '700px'],
                maxmin: true,
                end: function () {
                }
            });
        layer.full(index)
    }

    /**
     * 删除
     */
    function deletequota() {
        var rowDatas = searcherBS.getSelectedRows("quotaId");
        if (rowDatas.length < 1) {
            Dialog.errorMsg("请至少选择一行");
            return;
        }
        Dialog.confirm('确定删除？', function () {
            Page.ajaxPOST(ctxPath + "/quota/delete", {quotaId: rowDatas}, function () {
                Dialog.msg("操作成功", function () {
                    searcherBS.refresh();
                });
            });
        });
    }

    /**
     * 下载导入模板
     */
    function downloadTemplate() {
        window.location.href = ctxPath + "/impTemplate/quotaImpTemplate.xls";
    }

    /**
     * 显示上传文件弹框
     */
    function importByTemplate() {
        //上传窗口
        Dialog.open('文件上传', $("#uploadModal"), {area: ['500px', '300px']});
    }

    /**
     * 弹框取消
     */
    function closeDialog(){
        layer.closeAll();
    }

    /**
     * 上传模板数据
     */
    function uploadTemplateData() {
        let importFile = $("#importFile").val();
        if (!importFile) {
            Dialog.errorMsg("请上传文件！");
            return false;
        }
        let formData = new FormData();
        formData.append("importFile", document.getElementById('importFile').files[0]);
        layer.load(1, {shade: [0.3, '#393D49']});
        $.ajax({
            url: ctxPath + "/quota/importQuotaData",
            type: 'POST',
            dataType: "json",
            data: formData,
            cache: false, // 不缓存
            processData: false, //不对数据进行处理
            contentType: false, //不对Content-type进行处理
            success: function (data) {
                layer.closeAll();
                if (data.code == 500) {
                    Dialog.errorMsg(data.msg);
                } else if (data.materialList.length == 0 && data.vendorList.length == 0) {
                    Dialog.msg("成功导入 " + data.sum + " 行记录", null, {time: 3000});
                } else {
                    let str = "";
                    if (data.materialList.length != 0) {
                        str += "SRM不存在物料代码：<br>";
                        for (let i in data.materialList) {
                            str += data.materialList[i] + "<br>";
                        }
                    }
                    if (data.vendorList.length != 0) {
                        str += "SRM不存在供应商代码：<br>";
                        for (let i in data.vendorList) {
                            str += data.vendorList[i] + "<br>";
                        }
                    }
                    if (data.sum!=0){
                        str+="成功导入 "+data.sum+" 行数据";
                    }
                    layer.alert(str, {title: ""});
                }
            },
            error: function () {
                layer.closeAll();
                Dialog.errorMsg("上传失败！");
            }
        });
    }
</script>
</body>